package com.smartfarm.service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 系统日志记录器
 */
public class SystemLogger {
    private static final List<LogEntry> logs = new ArrayList<>();

    public static void logInfo(String source, String message) {
        log("INFO", source, message);
    }

    public static void logWarning(String source, String message) {
        log("WARNING", source, message);
    }

    public static void logError(String source, String message) {
        log("ERROR", source, message);
    }

    private static void log(String level, String source, String message) {
        LogEntry entry = new LogEntry(LocalDateTime.now(), level, source, message);
        logs.add(entry);
        System.out.printf("[%s] %s - %s: %s%n", 
            entry.timestamp, entry.level, entry.source, entry.message);
    }

    public static List<LogEntry> getLogs() {
        return new ArrayList<>(logs);
    }

    public static List<LogEntry> getLogsByLevel(String level) {
        List<LogEntry> filtered = new ArrayList<>();
        for (LogEntry entry : logs) {
            if (entry.level.equals(level)) {
                filtered.add(entry);
            }
        }
        return filtered;
    }

    public static List<LogEntry> getLogsBySource(String source) {
        List<LogEntry> filtered = new ArrayList<>();
        for (LogEntry entry : logs) {
            if (entry.source.equals(source)) {
                filtered.add(entry);
            }
        }
        return filtered;
    }

    public static void printLogs() {
        for (LogEntry entry : logs) {
            System.out.printf("[%s] %s - %s: %s%n", 
                entry.timestamp, entry.level, entry.source, entry.message);
        }
    }

    private static class LogEntry {
        private final LocalDateTime timestamp;
        private final String level;
        private final String source;
        private final String message;

        public LogEntry(LocalDateTime timestamp, String level, String source, String message) {
            this.timestamp = timestamp;
            this.level = level;
            this.source = source;
            this.message = message;
        }
    }
} 